"""
作者：高勇
日期：2025年02月05日
* 描述：ssh测试 
"""
import paramiko

def ssh_execute_command(host, port, username, password, command):
    # 创建SSH客户端
    client = paramiko.SSHClient()
    # 允许连接不在known_hosts中的主机
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        # 连接到SSH服务器
        client.connect(host, port=port, username=username, password=password)

        # 执行命令
        stdin, stdout, stderr = client.exec_command(command)

        # 获取命令输出
        output = stdout.read().decode()
        error = stderr.read().decode()

        return output, error
    finally:
        # 关闭连接
        client.close()

# 示例
host = '106.52.222.179'
port = 22
username = 'root'
password = 'Gzh123321.'
command = 'ls -l'

output, error = ssh_execute_command(host, port, username, password, command)
print("输出:", output)
print("错误:", error)